package com.suma.platform.core.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author suma
 * @date 2018/2/27 下午3:27
 */
public class NIOUtils {

	public static void readFile() {
		RandomAccessFile randomAccessFile = null;
		try {
			randomAccessFile = new RandomAccessFile("/Users/suma/logs/suma/output/member.log", "rw");
			FileChannel fileChannel = randomAccessFile.getChannel();
			ByteBuffer buf = ByteBuffer.allocate(1024);
			int byteRead = fileChannel.read(buf);
			System.out.println(byteRead);
			while (byteRead != -1) {
				buf.flip();
				while (buf.hasRemaining()) {
                    System.out.println((char)buf.get());
//					System.out.println(buf.get());
				}
				buf.compact();
				byteRead = fileChannel.read(buf);

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (randomAccessFile != null) {
				try {
					randomAccessFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

    public static void main(String[] args) {
        NIOUtils.readFile();
    }
}
